FROM php:7.4-cli
# Set working dir.
ARG CONTAINER_APP_DIR
WORKDIR ${CONTAINER_APP_DIR}

RUN uname -a \
    && sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list \
    && apt update
# Install php ext.
RUN apt-get install git zip unzip -y && \
    apt-get install libssl1.1 krb5-locales libkrb5-3 libgssapi-krb5-2 -y && \
    apt-get install libcurl4 libfreetype6-dev zlib1g-dev libwebp-dev libpng-dev -y && \
    apt-get install libxpm-dev libpcre3-dev libjpeg62-turbo-dev --allow-downgrades -y && \
    docker-php-ext-install sockets pcntl && \
    docker-php-ext-install pdo_mysql && \
    docker-php-ext-configure opcache --enable-opcache && \
    docker-php-ext-install opcache && \
    docker-php-ext-configure gd --with-webp=/usr/include/webp --with-freetype=/usr/include/freetype2 --with-jpeg=/usr/include && \
    docker-php-ext-install -j$(nproc) gd && \
    #pecl install redis && \
    #docker-php-ext-enable redis && \
    docker-php-ext-enable opcache && \
    apt-get install libssl-dev -y

RUN curl -fsSL 'https://mxefile.oss-cn-beijing.aliyuncs.com/redis-5.3.6.tgz' -o redis.tar.gz \
    && mkdir -p /tmp/redis \
    && tar -xf redis.tar.gz -C /tmp/redis --strip-components=1 \
    && rm redis.tar.gz \
    && docker-php-ext-configure /tmp/redis --enable-redis \
    && docker-php-ext-install /tmp/redis \
    && rm -r /tmp/redis

ARG INSTALL_LIB_EVENT
RUN if [ ${INSTALL_LIB_EVENT} = true ]; then \
    apt-get install libevent-dev -y && \
    curl -fsSL 'https://mxefile.oss-cn-beijing.aliyuncs.com/event-3.0.6.tgz' -o event.tar.gz \
    && mkdir -p /tmp/event \
    && tar -xf event.tar.gz -C /tmp/event --strip-components=1 \
    && rm event.tar.gz \
    && docker-php-ext-configure /tmp/event \
    && docker-php-ext-install /tmp/event \
    && rm -r /tmp/event \ 
   # && echo extension=event > /usr/local/etc/php/conf.d/event.ini \
;fi

RUN docker-php-source delete

# opcache
RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini
RUN sed -i 's/;opcache.enable=1/opcache.enable=1/' /usr/local/etc/php/php.ini
RUN sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/' /usr/local/etc/php/php.ini

# Install composer and set mirror.
# Reference: https://pkg.phpcomposer.com/#how-to-install-composer
# Referencr: https://developer.aliyun.com/composer
ARG INSTALL_COMPOSER
ARG COMPOSER_INSTALLER
ARG COMPOSER_MIRROR
RUN if [ ${INSTALL_COMPOSER} = true ]; then \
    php -r "copy('${COMPOSER_INSTALLER}', 'composer-setup.php');" && \
    php composer-setup.php && \
    php -r "unlink('composer-setup.php');" && \
    mv composer.phar /usr/local/bin/composer && \
    composer config -g repo.packagist composer ${COMPOSER_MIRROR} \
;fi

# Expose port
ARG CONTAINER_PORT
EXPOSE ${CONTAINER_PORT}
